<?

class reviews {



// FUNCTION // get all reviews
function get_reviews($params){


	$params = in($params);
	
	if($params['tree_id']) $where .= " AND tree_id = '".$params['tree_id']."' ";
	if($params['status']) $where .= " AND status = '".$params['status']."' ";
	if($params['limit']) $limit_sql .= " LIMIT ".$params['limit']." ";

	$sql = "SELECT review_id, review_date, status, tree_id, reviewer_name, email, county, trip_date, review_title, review, value, location, facilities, cleanliness, overall, user_ip
	FROM hotel_reviews
	WHERE '1' = '1'
	".$where."
	ORDER BY review_date DESC
	".$limit_sql."	";

	return apply_query($sql, $num_rows, $params['cache']);

}





// FUNCTION // get a single review
function get_review($params){
	
if(!empty($params['review_id'])){

	$sql = "SELECT review_id, review_date, status, tree_id, reviewer_name, email, county, trip_date, review_title, review, value, location, facilities, cleanliness, overall, user_ip
	FROM hotel_reviews
	WHERE review_id = '".$params['review_id']."'
	LIMIT 1";
	
	$result = mysql_query($sql) or die (mysql_error());
	$num_rows = mysql_num_rows($result);

	if($num_rows){
		$row = mysql_fetch_array($result, MYSQL_BOTH);
		$row = array_map('array_stripslashes', $row);
		return $row;
	}else{
		return false;
	}
	
}

}








// FUNCTION // get all reviews and get the average score
function get_review_average($params){


	$params = in($params);

	if(!empty($params['tree_id'])) $where .= " AND tree_id = '".$params['tree_id']."' ";
	if(!empty($params['status'])) $where .= " AND status = '".$params['status']."' ";

	if(isset($where)){
	
		$sql = "SELECT tree_id,
		COUNT(tree_id) AS num_reviews,
		ROUND(AVG(value)) AS value,
		ROUND(AVG(location)) AS location,
		ROUND(AVG(cleanliness)) AS cleanliness,
		ROUND(AVG(facilities)) AS facilities,
		ROUND(AVG(overall)) AS overall
		FROM hotel_reviews
		WHERE '1' = '1'
		".$where."
		GROUP BY tree_id";
	
		return apply_query($sql, $num_rows, $params['cache']);
		
	}

}





// FUNCTION // insert review details
function insert_review($params){


	$params = in($params);
	
	$sql = "INSERT INTO hotel_reviews
	SET	review_date = '".mktime()."',
	status = 'p',
	tree_id = '".$params['tree_id']."',
	reviewer_name = '".$params['reviewer_name']."',
	email = '".$params['email']."',
	trip_date = '".$params['trip_date']."',
	review_title = '".$params['review_title']."',
	review = '".$params['review']."',
	value = '".$params['value']."',
	location = '".$params['location']."',
	facilities = '".$params['facilities']."',
	cleanliness = '".$params['cleanliness']."',
	overall = '".$params['overall']."',
	user_ip = '".$params['user_ip']."' ";
	
	
	$result = mysql_query($sql) or die (mysql_error());
	
	return mysql_insert_id();

	
}



// FUNCTION // insert review details
function update_review($params){


	$params = in($params);

	$sql = "UPDATE hotel_reviews
	SET	status = '".$params['status']."',
	reviewer_name = '".$params['reviewer_name']."',
	review_title = '".$params['review_title']."',
	review = '".$params['review']."',
	value = '".$params['value']."',
	location = '".$params['location']."',
	facilities = '".$params['facilities']."',
	cleanliness = '".$params['cleanliness']."',
	overall = '".$params['overall']."'
	WHERE review_id = '".$params['review_id']."' ";
		
	$result = mysql_query($sql) or die (mysql_error());
	
	
	
}








// FUNCTION // Delete review
function delete_review($review_id){

if(!empty($review_id)){

	$sql = "DELETE
	FROM hotel_reviews
	WHERE review_id = ".$review_id."
	LIMIT 1 ";
	
	$result = mysql_query($sql) or die (mysql_error());

}

}







// FUNCTION // validate review form
function validate_review_form($form_data){

	$my_array = array();
	
	if(!$form_data['tree_id']) array_push($my_array, 'Critical error');
	if(!$form_data['month'] AND !$form_data['year']) array_push($my_array, 'Please enter the date you stayed at this hotel');
	if(!$form_data['location']) array_push($my_array, 'Please rate the cost of food and drink');
	if(!$form_data['value']) array_push($my_array, 'Please rate the value');
	if(!$form_data['cleanliness']) array_push($my_array, 'Please rate the cleanliness');
	if(!$form_data['facilities']) array_push($my_array, 'Please rate the facilities');
	if(!$form_data['overall']) array_push($my_array, 'Please give an overall rating');
	if(!$form_data['review_title']) array_push($my_array, 'Please give your review a title');
	if(!$form_data['review']) array_push($my_array, 'Please enter your review');
	if(!$form_data['reviewer_name']) array_push($my_array, 'Please enter your name');
	
	if(!$form_data['image_random_value_recived']){
		array_push($my_array, 'Please enter the validation code');
	}elseif($form_data['image_random_value'] !== md5($form_data['image_random_value_recived'])){
		array_push($my_array, 'You entered an incorrect validation code');
	}


	return $my_array;
	
}






// FUNCTION // validate review form at the back end
function validate_review_form_admin($form_data){

	$my_array = array();
	
	if(!$form_data['tree_id']) array_push($my_array, 'Critical error');
	if(!$form_data['location']) array_push($my_array, 'Please rate the cost of food and drink');
	if(!$form_data['value']) array_push($my_array, 'Please rate the value');
	if(!$form_data['cleanliness']) array_push($my_array, 'Please rate the cleanliness');
	if(!$form_data['facilities']) array_push($my_array, 'Please rate the facilities');
	if(!$form_data['overall']) array_push($my_array, 'Please give an overall rating');
	if(!$form_data['reviewer_name']) array_push($my_array, 'Please enter your name');
	if(!$form_data['review_title']) array_push($my_array, 'Please give your review a title');
	if(!$form_data['review']) array_push($my_array, 'Please enter your review');
	if(!$form_data['status']) array_push($my_array, 'Please enter the status');

	return $my_array;
	
}



// FUNCTION // insert review details
function move_update_review($params){


	$params = in($params);

	$sql = "UPDATE hotel_reviews
	SET tree_id = '".$params['move_to_tree_id']."'
	WHERE tree_id = '".$params['delete_tree_id']."' "; 
		
	$result = mysql_query($sql) or die (mysql_error());
	
	
	
}

// FUNCTION // Delete all reviews for a given tree id
function delete_reviews($tree_id){

	if(!empty($tree_id)){
	
		$sql = "DELETE
		FROM hotel_reviews
		WHERE tree_id = ".$tree_id." ";
		
		$result = mysql_query($sql) or die (mysql_error());
	
	}

}


}

?>